package com.yeshine.opendata.controller.admin;

import com.yeshine.opendata.entity.SysDictionary;
import com.yeshine.opendata.entity.SysModel;
import com.yeshine.opendata.service.ISysModelService;
import com.yeshine.opendata.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;



/**
 * Created by taohj on 2014/8/22.
 *
 */
@RequestMapping("admin/model")
@Controller
public class AdminSysModelController extends  AbstractAdminControllerBase{

    @Autowired
    public ISysModelService service;

    @RequestMapping("listV")
    public String  listV(){
        return "admin/model/list";
    }

    @ResponseBody
    @RequestMapping("list")
    public  String list(
            @RequestParam(value = "page",required = false) Integer page,
            @RequestParam(value = "rows",required = false) Integer size
    ){
        List<SysModel> list=service.getFunctionMenuTree();
        return list.toString();
    }


    @RequestMapping("createV")
    public  String createV( ){
        return "admin/model/create";
    }

    @ResponseBody
    @RequestMapping("create")
    public  String create(
            @RequestParam("name") String name,
            @RequestParam(value = "url",required = false) String url,
            @RequestParam(value = "type") Integer type,
            @RequestParam(value = "pid",required = false) Integer pid,
            @RequestParam(value = "isLeaf") Integer isLeaf,
            @RequestParam(value = "sort") Integer sort,
            @RequestParam(value = "description") String description
    ){
        SysModel model=new SysModel();
        model.setName(name);
        model.setUrl(url);
        model.setType(new SysDictionary(type));
        model.setParent(new SysModel(pid));
        model.setIsLeaf(new SysDictionary(isLeaf));
        model.setSort(sort);
        model.setDescription(description);
        Result<SysModel> result=service.create(model);
        return  result.toString();
    }


    @RequestMapping("modifyV")
    public  String modifyV(
            @RequestParam("id") Integer id
    ){
        SysModel model=service.getById(id).getTarget();
        setRequestAttr("model",model);
        return  "admin/model/modify";
    }

    @ResponseBody
    @RequestMapping("modify")
    public  String modify(
            @RequestParam("id") Integer id,
            @RequestParam("name") String name,
            @RequestParam(value = "url",required = false) String url,
            @RequestParam(value = "type") Integer type,
            @RequestParam(value = "pid",required = false) Integer pid,
            @RequestParam(value = "isLeaf") Integer isLeaf,
            @RequestParam(value = "sort") Integer sort,
            @RequestParam(value = "description") String description){

        SysModel model=service.getById(id).getTarget();
        model.setName(name);
        model.setUrl(url);
        model.setType(new SysDictionary(type));
        model.setParent(new SysModel(pid));
        model.setIsLeaf(new SysDictionary(isLeaf));
        model.setSort(sort);
        model.setDescription(description);
        Result<SysModel> result= service.modify(model);
        return  result.toString();
    }


    @ResponseBody
    @RequestMapping("remove")
    public  String remve(
            @RequestParam("id") Integer id
    ){
        SysModel model=new SysModel(id);
        Result<SysModel> result=service.remove(model);
        return  result.toString();
    }


}
